package com.rexolar.openapi.interceptor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.rexolar.openapi.constants.Constants;
import com.rexolar.openapi.constants.PublicParamsConstants;
import com.rexolar.openapi.context.OpenContext;
import com.rexolar.openapi.context.OpenContextUtil;
import com.rexolar.openapi.enums.ErrorCodeEnum;
import com.rexolar.openapi.exception.IllegalRequestException;
import com.rexolar.openapi.job.AppSyncJob;
import com.rexolar.openapi.utlis.ValidDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * @author zhusiyu11000
 * @description: 参数长度截器
 * @date 2020-01-09
 **/

@Component
@Slf4j
public class PublicParamsCheckInterceptor extends BaseOpenApiInterceptor {
    @Value("${requestExpTime:60}")
    private int requestExpTime;
    private static final int GET_PARAMS_LEN = 1024;

    @Autowired
    AppSyncJob appSyncJob;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) {

        OpenContext openContext = OpenContextUtil.getContext();
        if (null == openContext) {
            openContext = OpenContextUtil.enter(httpServletRequest);
        }
        String requestMethod = httpServletRequest.getMethod();

        //如果get请求参数大于1024则返回提醒使用post请求提交
        if (Constants.HTTP_METHOD_GET.equals(requestMethod) && httpServletRequest.getQueryString().length() > GET_PARAMS_LEN) {
            throw new IllegalRequestException(ErrorCodeEnum.ILLEGAL_PARAMS_LEN_MORE_1024);
        }

        checkPublicParams(openContext);

        return true;
    }

    private void checkPublicParams(OpenContext openContext) {
        Map<String, String> paramsMap = openContext.getParamsMap();

        if(paramsMap == null || paramsMap.isEmpty()){
            throw new IllegalRequestException(ErrorCodeEnum.LESS_PARAMETERS);
        }

        if(StringUtils.isBlank(paramsMap.get(PublicParamsConstants.TIMESTAMP))){
            log.error("PublicParamsCheckInterceptor$checkPublicParams APP_MISSING_TIMESTAMP, {}" , JSON.toJSONString(paramsMap.toString()));
            throw new IllegalRequestException(ErrorCodeEnum.LESS_PARAMETERS_TimeStamp);
        }
        if(!ValidDateUtil.isValidDate(paramsMap.get(PublicParamsConstants.TIMESTAMP), requestExpTime)){
            log.error("PublicParamsCheckInterceptor$checkPublicParams APP_INVALID_TIMESTAMP, {}" , JSON.toJSONString(paramsMap.toString()));
            throw new IllegalRequestException(ErrorCodeEnum.LESS_PARAMETERS_INVALID_TimeStamp);
        }
        // check appKey
        if(StringUtils.isBlank(paramsMap.get(PublicParamsConstants.APPKEY))){
            log.error("PublicParamsCheckInterceptor$checkPublicParams APP_MISSING_APPKEY, {}" , JSON.toJSONString(paramsMap.toString()));
            throw new IllegalRequestException(ErrorCodeEnum.LESS_PARAMETERS_APPKEY);
        }
        String appSecret = appSyncJob.getPartnerSecret(paramsMap.get(PublicParamsConstants.APPKEY));
        if(appSecret==null){
            log.error("PublicParamsCheckInterceptor$checkPublicParams USER_APP_STOP, appkey:{}" , paramsMap.get(PublicParamsConstants.APPKEY));
            throw new IllegalRequestException(ErrorCodeEnum.USER_APP_STOP);
        }
    }
}